package com.bci.pwtz.common.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil
{

    // 创建、设置Cookie
    public static void rocordAccountCookie(HttpServletRequest request, HttpServletResponse response, String uname, String upass)
    {
        try
        {
            // 创建Cookie
            Cookie nameCookie = new Cookie("username", uname);
            Cookie pwdCookie = new Cookie("userpass", upass);

            // 设置Cookie的生命周期
            nameCookie.setMaxAge(60 * 60 * 24 * 14);
            pwdCookie.setMaxAge(60 * 60 * 24 * 14);

            // set path
            nameCookie.setPath("/");
            pwdCookie.setPath("/");
            response.addCookie(nameCookie);
            response.addCookie(pwdCookie);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    // 清空账号Cookie
    public static void removeAccountCookie(HttpServletRequest request, HttpServletResponse response)
    {
        javax.servlet.http.Cookie[] cookies = request.getCookies();
        javax.servlet.http.Cookie c = null;
        if (cookies != null)
        {
            // 删除
            for (int i = 0; i < cookies.length; i++)
            {
                c = cookies[i];
                if (c != null)
                {
                    if (c.getName().equalsIgnoreCase("username"))
                    {
                        c.setValue(null);
                        c.setMaxAge(0);
                        c.setPath("/");
                        response.addCookie(c);
                    }
                    if (c.getName().equalsIgnoreCase("userpass"))
                    {
                        c.setValue(null);
                        c.setMaxAge(0);
                        c.setPath("/");
                        response.addCookie(c);
                    }
                }
                c = null;
            }
        }

    }

    // 是否记住了 账号
    public boolean hasAccountCookie(HttpServletRequest request)
    {
        javax.servlet.http.Cookie[] cookies = request.getCookies();
        javax.servlet.http.Cookie c = null;
        if (cookies == null)
            return false;
        // 删除
        for (int i = 0; i < cookies.length; i++)
        {
            c = cookies[i];
            if (c != null)
            {
                if (c.getName().equalsIgnoreCase("username") && c.getValue() != null)
                {
                    return true;
                }

            }
        }
        return false;

    }

    public String[] getCookieAccount(HttpServletRequest request)
    {
        // 是否有记录cookie

        javax.servlet.http.Cookie[] cookies = request.getCookies();
        int j = 0;
        String Original_CookiesName = null;
        String Original_CookiesPwd = null;

        if (cookies != null)
        {
            // 读取cookie
            javax.servlet.http.Cookie c = null;
            for (int i = 0; i < cookies.length; i++)
            {
                c = cookies[i];
                if (c != null)
                {

                    if (c.getName().equalsIgnoreCase("username") && c.getValue() != null)
                    {
                        Original_CookiesName = c.getValue();
                        j = j + 1;
                    }
                    if (c.getName().equalsIgnoreCase("userpass") && c.getValue() != null)
                    {
                        Original_CookiesPwd = c.getValue();
                    }
                }
                c = null;
            }
        }
        return new String[]
        { Original_CookiesName, Original_CookiesPwd };
    }

}
